
/*document.write('<script  type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>');
 */// var baseurl = "http://192.168.1.195:8083";
var baseurl = "http://lead.taiwuyou.cn/chint-dl";   //接口地址
//var baseurl ="http://1001.blistening.cn"; 
var pageurl = "http://lead.taiwuyou.cn/chint-dl";  //页面

//var programId = 347;
var channel = getUrlParam("channel");
var programlist = [];
var platSource = "gzh",
platform = 1;
var containbuyed;

function  get(url, data, callback) {
	$.ajax({
		url: /*baseurl +*/ url,
		type: 'GET',
		contentType: "application/json;charset=UTF-8",
		data: JSON.stringify(data),
		dataType: "json",
		async: false, //false同步  true异步
		success: function (res) {
			// if (res.code == 0) {
			console.log("请求成功");           
			callback(res);
			// console.log(JSON.stringify(res));
			// } else {
			//     console.log(res.message); 
			//     if (res.message == undefined) {
			//         console.log(res.message);                 
			//     } else {
			//         console.log(res.message);                 
			//     }
			// }

		},
		error: function (res) {
			console.log("出错了");  
			console.log(res);
			console.log(res.message);      
		},
		complete: function (xhr, res) {
			console.log(res);
			console.log(res.message);

		},
	})
}
function post(url, data, callback) {
	$.ajax({
		url: /*baseurl +*/ url,
		type: 'POST',
		contentType: "application/json;charset=UTF-8",
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		success: function (res) {
			// if (res.code == 0) {
			console.log("请求成功");
			callback(res);
			// console.log(JSON.stringify(res));
			// } else {
			//  console.log(res.message); 
			// if (res.message == undefined) {
			//     console.log(res.message);                 
			// } else {
			//     console.log(res.message);                 
			// }
			// }
		},
		error: function (res) {           
			console.log('出错了');
			console.log(res)
			console.log(res.message);         
		},

		complete: function (xhr, res) {
			console.log(res);
			// console.log(res.message);

		},
	})
}
//页面传参
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

//校验参数
function valiParams(param){
	if(param==null||param==""||param==undefined||param.length==0){
		return true;
	}else{
		return false;
	}
}
//获取缓存
function setStorage(key, value) {
	return window.localStorage.setItem(key, value);
}
//保存缓存
function getStorage(key) {
	return window.localStorage.getItem(key);
}
//排序
function sortBy(attr, rev) {
	//第二个参数没有传递 默认升序排列
	if (rev == undefined) {
		rev = 1;
	} else {
		rev = (rev) ? 1 : -1;
	}

	return function (a, b) {
		a = parseFloat(a[attr]);
		b = parseFloat(b[attr]);
		if (a < b) {
			return rev * -1;
		}
		if (a > b) {
			return rev * 1;
		}
		return 0;
	}
}
//封装禁止页面滚动方法（该方法兼容PC端和移动端）
function stopBodyScroll(isFixed) {
	var bodyEl = document.body;
	var top1 = 0;
	console.log(isFixed);
	if (isFixed) {
		top1 = window.scrollY
		bodyEl.style.position = 'fixed'
			bodyEl.style.top = -top1 + 'px'
	} else {
		bodyEl.style.position = ''
			bodyEl.style.top = ''
				window.scrollTo(0, top1) // 回到原先的top
	}

}
//截取字符串并拼接...省略号
function subStr(str, numbers) {
	var subStr="";
	if (str.length < numbers) {
		subStr = str.substring(0, numbers)
		return subStr;
	} else {
		subStr = str.substring(0, numbers)
		return subStr.concat("...");
	}

}

//动态加载js
function loadScript(src, callback) {
	var script = document.createElement('script'),
	head = document.getElementsByTagName('head')[0];
	script.type = 'text/javascript';
	script.charset = 'UTF-8';
	script.src = src;
	if (script.addEventListener) {
		script.addEventListener('load', function () {
			callback();
		}, false);
	} else if (script.attachEvent) {
		script.attachEvent('onreadystatechange', function () {
			var target = window.event.srcElement;
			if (target.readyState == 'loaded') {
				callback();
			}
		});
	}
	head.appendChild(script);
}


//toFixed兼容方法
Number.prototype.toFixed = function (n) {
	if (n > 20 || n < 0) {
		throw new RangeError('toFixed() digits argument must be between 0 and 20');
	}
	var number = this;
	if (isNaN(number) || number >= Math.pow(10, 21)) {
		return number.toString();
	}
	if (typeof (n) == 'undefined' || n == 0) {
		return (Math.round(number)).toString();
	}
	var result = number.toString();
	var arr = result.split('.');
	// 整数的情况
	if (arr.length < 2) {
		result += '.';
		for (var i = 0; i < n; i += 1) {
			result += '0';
		}
		return result;
	}
	var integer = arr[0];
	var decimal = arr[1];
	if (decimal.length == n) {
		return result;
	}
	if (decimal.length < n) {
		for (var i = 0; i < n - decimal.length; i += 1) {
			result += '0';
		}
		return result;
	}
	result = integer + '.' + decimal.substr(0, n);
	var last = decimal.substr(n, 1);
	// 四舍五入，转换为整数再处理，避免浮点数精度的损失
	if (parseInt(last, 10) >= 5) {
		var x = Math.pow(10, n);
		result = (Math.round((parseFloat(result) * x)) + 1) / x;
		result = result.toFixed(n);
	}
	return result;
};

var redirect_uri = window.location.href;
/*alert(redirect_uri);*/
//微信授权
var weixin = {
		config: {
			url:'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx487b598049f3a162&redirect_uri='+encodeURIComponent(redirect_uri)+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect',
			//ttbt_userInfo:JSON.parse(window.localStorage.getItem('ttbt_userinfo')),
			api:baseurl
		},

		isweixin: function() {
			var ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger'){
				return true;
			} else {
				return false;
			}
		},
		isMobile:function(){
			var ua = navigator.userAgent.toLowerCase(); 
			if (/iphone|ipad|ipod/.test(ua)) {
				platform = 0;               
			} else if (/android/.test(ua)){
				platform = 1;                             
			} else{
				platform = 1;

			}  
		}, 

		getQueryString: function(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
			var r = window.location.search.substr(1).match(reg);
			if (r!=null) return unescape(r[2]); return null;
		},

		getUser:function(code) {
			weixin.isMobile();
			var data = {            
					//"channel": channel,
					"code": code
					//"platSource": platSource,
					//"platform": platform
			}
			$.ajax({
				type: 'POST',
				url: weixin.config.api + '/rest/h5_auth_login',
				data:JSON.stringify(data),
				contentType: "application/json;charset=UTF-8",
				dataType: "json",
				async: false, //false同步  true异步
				success: function(res){
					//console.log(res);
					if (res.code==0) {
						window.localStorage.setItem('ttbt_userInfo',JSON.stringify(res));
					}        
				},
				error: function(res) {
					console.log(res);
				}
			});
		},

}

function getUserInfo(){
	
	var ttbt_userInfo =  window.localStorage.getItem('ttbt_userInfo');
	//alert(ttbt_userInfo);

	if(ttbt_userInfo != null){
		
		var user = JSON.parse(ttbt_userInfo);
		var nickname = user.nickname;
		var head_url=user.head_url;
		var openid = user.openid;
		$("#head_url_id").html("<img src='"+head_url+"' />");
		$("#niceName").html("<span>"+nickname+"</span>");
		$("#openid").val(openid);
		
		/**if (weixin.config.ttbt_userInfo.openid!=undefined ||weixin.config.ttbt_userInfo.openid!=null) {
			return JSON.parse(window.localStorage.getItem('ttbt_userInfo'));
		}  */         
	}else{
		if(weixin.getQueryString('code') != null){ 
			weixin.getUser(weixin.getQueryString('code'));
			
			var ttbt_userInfo =  window.localStorage.getItem('ttbt_userInfo');
			var user = JSON.parse(ttbt_userInfo);
			var nickname = user.nickname;
			var head_url=user.head_url;
			var openid = user.openid;
			$("#head_url_id").html("<img src='"+head_url+"' />");
			$("#niceName").html("<span>"+nickname+"</span>");
			$("#openid").val(openid);
			//return JSON.parse(window.localStorage.getItem('ttbt_userInfo'));
		}else{
			window.location.href = weixin.config.url;
		}
	}     
}

$(function(){
	var ttbt_userInfo = getUserInfo();
	
	
	//总共三个域名
	//shzstrtt.cn,ttfjl.cn,ykmsl.cn
	/**if(ttbt_userInfo.id!=null){
         var whichUrl=weixin.getQueryString("whichUrl");//判断跳转页面
            if(whichUrl==0){
                window.location.href="http://shzstrtt.cn/channelAutoLogin.html?from=wx&channelID=1000&userIdentity="+ttbt_userInfo.id;
                //window.location.href="http://ttfjl.cn/channelAutoLogin.html?from=wx&channelID=1000&userIdentity="+ttbt_userInfo.id;
                // window.location.href="http://ykmsl.cn/channelAutoLogin.html?from=wx&channelID=1000&userIdentity="+ttbt_userInfo.id;
            }
     }*/
});
